<template>
	<view class="">
		<u-navbar :is-back="true" :custom-back="goBack" title="统一设置" back-text="">
			
		</u-navbar>
		<u-form :model="form" ref="uForm">
			<view class="">
				计费方式
			</view>
			<u-form-item v-for="(item,index) in dataShowList" :key="index" style="height: 2.4rem;">
				<view @click="selectClickM(item,index)" class="Icon-bottom" style="display: flex;align-items: center;">
					<image v-if="item.value.tagType == false" style="width: 1em; height: 1em;margin: 10px;" src="../../style/image/cargo/kuang.png" alt="" />
					<image v-else style="width: 1em; height: 1em;margin: 10px;" src="../../style/image/cargo/gou.png" alt="" />
				</view>
				<text>标签</text>
				<u-input v-model="item.value.price==0?'':item.value.price" type="number" placeholder="输入金额" style="margin-left: 5px;" />
				<text>元/<text>{{item.key}}</text></text>
			</u-form-item>
			
			<u-form-item label="标签:" style="position: relative;">
				<picker @change="bindPickerChange" :value="indexTip" :range="arrayTip">
					<view class="uni-input" style="margin-left: 1rem;">{{arrayTip[indexTip]}}</view>
					<u-icon name="arrow-down" style="position: absolute;top: 1.1rem;left: 5.2rem;"></u-icon>
				</picker>
			</u-form-item>
			
			<u-form-item style="height: 2.4rem;margin-bottom: 1rem;">
				<view class="">备注：</view>
				<u-input style="margin-left: 1rem;" v-model="form.note" type="number" placeholder="请输入备注信息" />
			</u-form-item>
			
			<view class="" style="border-bottom: 1px solid #f3f4f7;">
				<view v-for="(item,index) in snList" :key="index" class="" style="padding-bottom: 1em;">
					设备SN:<text style="margin-left: 0.5rem;">{{item.sn}}</text>
				</view>
			</view>
			
		</u-form>
		<view class="button">
			<u-button @click="sureBtnClick" type="success" style="border-radius: 0; position: fixed;bottom: 0;width: 100%;">确认</u-button>
		</view>
	</view>
</template>

<script lang="js">
	import { findTimeQuantum,savePowerWire } from "../../utils/uniApi";
	export default {
		data() {
			return {
				snList: [],//sn数据list
				webId: '',
				
				dataShowList:[],//接口数据源转化数组
				dataDic:{},//接口数据源
				
				arrayTip: ['火爆', '折扣', '优惠'],
				indexTip: 0,
				tipCode:'ARE_POPULAR',//标签类型（DISCOUNT -> 折扣；ARE_POPULAR -> 火爆； PRIVILEGE -> 优惠）
				form: {
					index: 0,
					name: '',
					intro: '',
					sex: '',
					note:'',
				},
				checkboxList: [{
					name: '标签',
					checked: false,
					disabled: false
				}],
				
			};
		},
		onLoad(option) {
			if (option.data) {
				var dic = JSON.parse(option.data)
				this.snList = dic.list
				this.webId = dic.webId
			}
		},
		onLaunch(){
			uni.stopPullDownRefresh()
		},
		onShow() {
			this.getdata()
		},
		methods: {
			// 注意返回值为一个数组，单列时取数组的第一个元素即可(只有一个元素)
			
			goBack() {
				uni.navigateBack({
					delta: 1
				});
			},
			
			bindPickerChange: function(e) {
				////标签类型（DISCOUNT -> 折扣；ARE_POPULAR -> 火爆； PRIVILEGE -> 优惠）
				this.indexTip = e.target.value
				if(this.indexTip == 0){
					this.tipCode = 'ARE_POPULAR'
				} else if(this.indexTip == 1){
					this.tipCode = 'DISCOUNT'
				} else if(this.indexTip == 2){
					this.tipCode = 'PRIVILEGE'
				}
			},
			
			selectClickM(item,index){
				item.value.tagType = !item.value.tagType
				this.dataDic[item.key].tagType = item.value.tagType
			},
			//拉去网络数据
			getdata(){
				uni.showLoading({
					title: "加载中",
				});
				let params = {
					userId: uni.getStorageSync("userId") == "" ? "" : parseInt(uni.getStorageSync("userId")),
					sn: this.snList[0].sn,
				}
				findTimeQuantum(params).then((res) => {
					uni.hideLoading();
					if (res.data.code == 200) {
						this.dataDic = res.data.data
						for(var p in this.dataDic){
							if(this.dataDic.hasOwnProperty(p)){
								var dicT = {key:p,value:this.dataDic[p]}
								this.dataShowList.push(dicT)
							}
						}
					} else {
						uni.showToast({
							title: "请求失败",
							icon: 'none',
							duration: 2000
						});
					}
				})
			},
			//提交数据
			sureBtnClick(){
				uni.showLoading({
					title: "加载中",
				});
				let params = {
					userId: uni.getStorageSync("userId") == "" ? "" : parseInt(uni.getStorageSync("userId")),
					shopBranchId: this.webId,
					tagType:this.tipCode,//标签类型（DISCOUNT -> 折扣；ARE_POPULAR -> 火爆； PRIVILEGE -> 优惠）
					snsJson:JSON.stringify(this.snList),
					chargerPriceJson:JSON.stringify(this.dataDic),
				}
				savePowerWire(params).then((res) => {
					uni.hideLoading();
					if (res.data.code == 200) {
						uni.showToast({
							title: "设置成功",
							icon: 'none',
							duration: 2000
						});
						setTimeout(()=>{
							uni.navigateBack({
							  delta: 4,
							});
						},2000)
					} else {
						uni.showToast({
							title: "请求失败",
							icon: 'none',
							duration: 2000
						});
					}
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	page {
		background-color: #f3f5f7;
	}

	.u-form {
		background-color: #fff;
		padding: 35rpx;
		margin: 25rpx;
		border-radius: 10rpx;
	}
</style>
